package com.wlq.demo.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.wlq.demo.entity.AjaxResult;
import com.wlq.demo.uitls.PdfUtil;
import com.wlq.demo.uitls.RedisHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.*;

/**
 * @author csb
 * @description: TODO
 * @date 2024/10/8 11:19
 */
@RestController
@RequestMapping("/test")
public class testController {

    private static final String CHUNK_PREFIX = "big_file_";
    private static Long CHUNK_SIZE = 1024 * 1024L;
    private static final String remoteRootPath = "D:\\test\\big_file";

    @Autowired
    private RedisHelper redisHelper;

    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    private AjaxResult upload(@RequestParam(name = "file") MultipartFile file,
                              @RequestParam(name = "chunkIndex") Integer chunkIndex,
                              @RequestParam(name = "md5") String md5,
                              @RequestParam(name = "totalFileSize") Long totalFileSize,
                              @RequestParam(name = "fileName") String fileName) throws IOException {
        //创建存放文件夹
        String dirPath = remoteRootPath + "/" + "video" + "/" + md5 + "/";
        File dirFile = new File(dirPath);
        if (!dirFile.exists()){
            dirFile.mkdirs();
        }
        //分区文件
        String relativeFilePath = dirPath + fileName;
        File tempFile = new File(relativeFilePath);
        RandomAccessFile rw = new RandomAccessFile(tempFile, "rw");
        //定位到分片的偏移量
        rw.seek(CHUNK_SIZE * chunkIndex);
        //写入分片数据
        rw.write(file.getBytes());
        //关闭流
        rw.close();
        //读取已经分片的集合
        Set<Object> hasChunkList = new HashSet<>();
        String hasChunkKey = CHUNK_PREFIX + md5;
        if (redisHelper.hasKey(hasChunkKey)){
            Object o = redisHelper.get(hasChunkKey);
            JSONArray array = JSONUtil.parseArray(o);
            hasChunkList.addAll(array);
        }
        hasChunkList.add(chunkIndex);
        //最新分片下标跟新到redis
        redisHelper.set(hasChunkKey,hasChunkList);

        HashMap<String, Object> map = new HashMap<>();
        map.put("url",relativeFilePath);
        map.put("hasList",hasChunkList);
        return AjaxResult.success(map);
    }

    @GetMapping("/ss")
    public AjaxResult ss() throws IOException {
        String file = "E:\\test\\2\\3.pdf";
//        PdfUtil.WatermarkPDF(file);
        PdfUtil.getPdfSs(file);
        return AjaxResult.success("1");
    }

    @GetMapping("/getChatHistory")
    public AjaxResult getChatHistory(String name){
        Object o = redisHelper.get(name);
        if (null == o) {
            return AjaxResult.success("获取成功");
        }else {
            JSONArray list = JSONUtil.parseArray(o);
            return AjaxResult.success(list);
        }
    }
}
